package com.kaiyidiannao.adapter;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.kaiyidiannao.activity.tour.TourLineDetailActivity;
import com.kaiyidiannao.common.util.DateTimeUtils;
import com.kaiyidiannao.common.util.DisplayUtil;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.Comment;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 大志 on 14-4-4.
 * 凑对去 - 评论
 */
public class TourCommentAdapter extends BaseAdapter {
    private ImageLoader imageLoader;
    private Context context;
    private LayoutInflater layoutInflater;
    private List<Comment> tourCommentList = new ArrayList<Comment>();
    private boolean flag; //人名位置,微记模块填写true,别的模块false

    public TourCommentAdapter(Context context, List<Comment> list, boolean flag) {
        this.context = context;
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.tourCommentList = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.flag = flag;
    }

    @Override
    public int getCount() {
        return tourCommentList == null ? 0 : tourCommentList.size();
    }

    @Override
    public Object getItem(int position) {
        return tourCommentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        if (v == null) {
            v = layoutInflater.inflate(R.layout.tour_comment, null);
        }
        try {
            final Comment comment = (Comment) getItem(position);

//                View v = View.inflate(DataCenter.context, R.layout.tour_comment, null);
            //填评论内容
            ((TextView) v.findViewById(R.id.tour_comment_context)).setText(comment.getContent());
            //发表评论的人
            ((TextView) v.findViewById(R.id.tour_comment_title)).setText(comment.getUsername());
            if(flag) {
                ((TextView) v.findViewById(R.id.tour_comment_title)).setGravity(Gravity.RIGHT);
            }
            //填评论时间
            ((TextView) v.findViewById(R.id.tour_comment_time)).setText("发表于: " + DateTimeUtils.timeFormat(comment.getCommentTime()));
            //为评论键绑定监听器
            v.findViewById(R.id.tour_comment_revert_btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转到评论页面
                    TourLineDetailActivity activity = (TourLineDetailActivity) context;
                    String pid = comment.getItemid();
                    activity.startSubCommentActivity(pid);
                }
            });
            //调节间距
            if (position == getCount() - 1) {

            }

            //子评论
            List<Comment> subComments = comment.getReply();
            ((LinearLayout) v.findViewById(R.id.tour_subcomment_layout)).removeAllViews();
            if (subComments != null) {
                for (int j = 0; j < subComments.size(); ++j) {
                    Comment subComment = subComments.get(j);
                    LinearLayout subView = (LinearLayout) layoutInflater.inflate(R.layout.tour_commnet_sub, null);
                    //View subView = View.inflate(DataCenter.context, R.layout.tour_commnet_sub, null);
                    //如果不匿名则填入子评论用户名 & 用户头像
                    //填子人名称
                    ((TextView) subView.findViewById(R.id.tour_comment_title)).setText(subComment.getUsername());
                    //填子评论内容
                    ((TextView) subView.findViewById(R.id.tour_comment_context)).setText(subComment.getContent());
                    //填子评论时间
                    ((TextView) subView.findViewById(R.id.tour_comment_time)).setText("发表于: " + DateTimeUtils.timeFormat(subComment.getCommentTime()));
                    //在当前评论中加入它的子评论
                    ((LinearLayout) v.findViewById(R.id.tour_subcomment_layout)).addView(subView);
                    // 设置间距
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) subView.getLayoutParams();
                    int dp = DisplayUtil.dip2px(30, context.getResources().getDisplayMetrics().density);
                    params.setMargins(0, 0 ,0, dp);
                    if (j != subComments.size() - 1) {
                        subView.setLayoutParams(params);
                    }

                }
            }

            int num = ((LinearLayout) v.findViewById(R.id.tour_subcomment_layout)).getChildCount();
            Log.d("CommentAdapter---", "CommentAdapter:" + num);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return v;
    }
}
